<?php

class Zen_File
{

    protected static $_file;


    public static function exists($file)
    {
        // no file requested?
        $file = trim($file);
        if (! $file) {
            return false;
        }

        // using an absolute path for the file?
        // dual check for Unix '/' and Windows '\',
        // or Windows drive letter and a ':'.
        $abs = ($file[0] == '/' || $file[0] == '\\' || $file[1] == ':');
        if ($abs && file_exists($file)) {
            return $file;
        }

        // using a relative path on the file
        $path = explode(PATH_SEPARATOR, ini_get('include_path'));
        foreach ($path as $base) {
            // strip Unix '/' and Windows '\'
            $target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $file;
            if (file_exists($target)) {
                return $target;
            }
        }
        // never found it
        return false;
    }


    /**
     *
     * Uses [[php::include() | ]] to run a script in a limited scope.
     *
     * @param string $file The file to include.
     *
     * @return mixed The return value of the included file.
     *
     */
    public static function load($file)
    {
        Zen_File::$_file = Zen_File::exists($file);

        if (! Zen_File::$_file) {
            return false;
        }

        // clean up the local scope, then include the file and
        // return its results.
        unset($file);
        return include Zen_File::$_file;
    }

}